﻿using System.Web.Mvc;
using lindyaka.Models.Administration;
using lindyaka.Services.Administration;

namespace lindyaka.Web.Areas.Administration.Controllers
{
    public class RolesController : Controller
    {
        private readonly Roles _roles;

        public RolesController()
        {
            _roles = new Roles();
        }

        //
        // GET: /Administration/Panel/
        public ActionResult Index()
        {
            return View(_roles.Get());
        }

        //
        // GET: /Administration/Panel/Details/5
        public ActionResult Details(int id)
        {
            return View(_roles.Get(id));
        }

        //
        // GET: /Administration/Panel/Create
        public ActionResult Create()
        {
            return View(new Role());
        }

        //
        // POST: /Administration/Panel/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var role = new Role();
            TryUpdateModel(role);
            if (ModelState.IsValid)
            {
                _roles.Add(role);
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Administration/Panel/Edit/5
        public ActionResult Edit(int id)
        {
            return View(_roles.Get(id));
        }

        //
        // POST: /Administration/Panel/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Role role = _roles.Get(id);
            TryUpdateModel(role);
            if (ModelState.IsValid)
            {
                _roles.Update(role);
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Administration/Default/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_roles.Get(id));
        }

        //
        // POST: /Administration/Default/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Role role = _roles.Get(id);
            try
            {
                _roles.Remove(role);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(role);
            }
        }
    }
}